/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rules;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import types.ColoringInfo;
import types.ISudokuMatrix;
import utils.RuleHelper;

/**
 *
 * @author ali
 */
public class RowTwoFree implements Rule{

    int quadrat = 0;

    Collection checkedBlocks = new HashSet();
    Collection checkedCols = new HashSet();
    Collection checkedRows =new HashSet();
    
    public String getName() {
        return RowTwoFree.class.getSimpleName();
    }

    public String getPicName() {

         return "Überprüfe ob in einer Zeile 2 felder leer sind. Wenn Ja überprüfe ob " +
                "in der Spalte bzw. \nQuadrat der leeren Felder eine vermisste Zahl vorkommt. " +
                "Wenn ja schließe diese aus.";
       
    }

    public boolean isApplicable(int x, int y, ISudokuMatrix matrix) {
        System.out.println("Trying to apply rule "+ this.getName()+ " for "+x+","+y);
        try {
            this.setQuadrat(x, y);
            int[] digits = RuleHelper.getDigittsSameRow(x, matrix);

            Collection missedDigits = RuleHelper.
                    getMissingdigitsInQuadratOrColOrRow(RuleHelper.intArrayToCollection(digits));

            this.checkedRows.add(x);

            System.out.print("missed digits in row "+x+": ");
            RuleHelper.printCollection(missedDigits);


            if (missedDigits.size() > 2) {
                return false;
            }

            for (int i = 0; i<digits.length;i++)
            {
                if (digits[i]!=0)
                    continue;

                Iterator iter = missedDigits.iterator();
                while (iter.hasNext())
                {
                    Integer a = (Integer) iter.next();
                    if (RuleHelper.existsDigitInCol(a, i, matrix))
                    {
                        this.checkedCols.add(i);
                    }
                    if(RuleHelper.existsDigitInQuadrat(a,x,i,matrix))
                    {
                        this.checkedBlocks.add(RuleHelper.calculateQuadratNumber(x, i));
                    }
                    if (RuleHelper.existsDigitInCol(a, i, matrix)
                            ||RuleHelper.existsDigitInQuadrat(a,x,i,matrix))
                    {
                        System.out.println("\n\t"+a+" exist in col "+ i + " and/or in Quadrat: "
                                +RuleHelper.calculateQuadratNumber(x ,i));
                        System.out.println("\tRule applicable");
                        return true;
                    }

                   
                }
            }

        } catch (Exception ex) {
            Logger.getLogger(ColumnTwoFree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;

    }

    public int getWeight(int QuadratNumber) {
        if (QuadratNumber==2 || QuadratNumber==5 || QuadratNumber==8)
        {
            //5 für spalte selbst +
            //10 für zwei zeilen oder 2 Quadraten
            return (5+10)*2;
        }
        else
            //6 für spalte selbst +
            //10 für zwei zeilen oder 2 Quadraten
            return (6+10)*2;
    }

    public int compareTo(Rule o) {
        if (this.getWeight(this.getQuadrat())<o.getWeight(o.getQuadrat()))
            return -1;
        else if (this.getWeight(this.getQuadrat())>o.getWeight(o.getQuadrat()))
            return 1;
        else
            return 0;
    }
    public void setQuadrat(int x, int y) throws Exception {

            this.quadrat = RuleHelper.calculateQuadratNumber(x, y);

    }

    public int getQuadrat() {
       return this.quadrat;
    }

    public ColoringInfo getCheckedUnits(int x, int y, ISudokuMatrix matrix) {
        ColoringInfo ci = new ColoringInfo();

        ci.setBlocks(this.checkedBlocks);
        ci.setColums(this.checkedCols);
        ci.setRows(this.checkedRows);

        return ci;
    }
}
